package controllers;

import play.*;
import play.data.validation.Required;
import play.data.validation.Valid;
import play.libs.Mail;
import play.mvc.*;
import sun.tools.jar.resources.jar;

import java.util.*;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import models.*;

public class Organizations extends Controller {

	/**
	 * renders the html page for creating organization
	 * 
	 * @author Sara Assem
	 */
	public static void createOrganization() {

		render();
	}

	/**
	 * creates a new organization
	 * 
	 * @author Sara Assem
	 * @params Organization o id of the organization
	 */
	public static void organizationCreated(Organization o) {
		o.creationDate = new Date();
		boolean b = Organization.checkNameDuplication(o.name, o.id);
		if (b == false) {
			flash.error("This name already existed");
			Organizations.createOrganization();
		}
		String idstring = session.get("userId");
		if (idstring == null) {
			Home.home(null, null, "Please Login");
		}
		Long id1 = Long.parseLong(idstring);
		User user = User.findById(id1);
		o.lead = user;
		user.UsersOrg.add(o);
		user.organization.add(o);

		o.save();

		user.save();

		Permission permission = new Permission("organizationLead", o, user,
				null).save();

		User.userPermissionInOrganization(user.id, o.id);

		addSubscriber(o.id);
		showOrganization(o.id);

	}

	public static void orgMembers(Long organizationId) {
		Organization organization = Organization.findById(organizationId);
		List<User> orgMembers = organization.orgMembers;
		render(orgMembers);
	}

	/**
	 * 
	 * shows organization using its organization ID added a try and catch
	 * 
	 * @author Sara Assem
	 * @author Shahir El Faramawy
	 * @params long id id of the organization
	 */
	public static void showOrganization(Long id) {
		boolean canInviteIdeaDeveloper = true;

		Organization organization = Organization.findById(id);
		String idstring = session.get("userId");

		if (idstring == null) {
			Home.home(null, null, "Please Login in");
		}
		Long userId = Long.parseLong(idstring);
		User user = User.findById(userId);

		boolean b = organization.userinSubscribers(user);
		List<User> alluser = organization.getSubscribers();
		boolean check = organization.checkUser(user);

		boolean isBlocked = false;
		for (int i = 0; i < organization.blockedUsers.size(); i++) {
			if (user.equals(organization.blockedUsers.get(i)))
				isBlocked = true;
		}
		boolean isFound = false;
		for (int j = 0; j < organization.getEntities().size(); j++) {
			Entities entity = organization.getEntities().get(j);
			for (int k = 0; k < entity.entityOrganizers.size(); k++) {
				if (user.equals(entity.entityOrganizers.get(k)))
					isFound = true;
			}
		}
		organization.save();

		boolean requested = false;

		for (int i = 0; i < organization.requesters.size(); i++) {
			if (organization.requesters.get(i) == user) {
				requested = true;
			}
		}
		boolean blockRequest = false;
		for (int i = 0; i < organization.blockedRequesters.size(); i++) {
			if (organization.blockedRequesters.get(i) == user)
				blockRequest = true;
		}

		if (organization.checkUser(user) == false) {
			Permission Role = new Permission("ideaDeveloper", organization,
					user, null);

			Role.save();
		}
		String userType = "";
		Permission permission = Permission.find("user=? AND aUserType=?", user,
				"systemAdmin").first();
		if (permission != null) {
			userType = permission.aUserType;
		}
		if (permission == null && userType == "") {
			permission = User.userPermissionInOrganization(userId, id);

		}
		if (permission != null)
			userType = permission.aUserType;
		else
			userType = "ideaDeveloper";

		if (userType.equals("organizer")) {
			Permission permissionOfOrganizer = User
					.userPermissionInOrganization(user.id, organization.id);
			canInviteIdeaDeveloper = permissionOfOrganizer.inviteToBeIdeaDeveloper;

		}

		render(organization, b, user, alluser, check, userType, isBlocked,
				isFound, requested, blockRequest, canInviteIdeaDeveloper);

		renderBinary(organization.picture.get());

	}

	/**
	 * Renders the profile image of the organization
	 * 
	 * @param organizationId
	 *            to get the image saved of specific organization
	 * @author Mohamed Abdel Kader
	 */

	public static void showImage(Long organizationId) {

		Organization organization = Organization.findById(organizationId);
		renderBinary(organization.picture.get());

	}

	/**
	 * Gets all the organizations on the system and the organiztions the user
	 * created, joined and subscribed
	 * 
	 * @author Mohamed Abdelkader
	 */

	public static void showAllOrganizations() {

		List<Organization> organizationList = Organization.getOrganizations();
		String idstring = session.get("userId");
		if (idstring == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(idstring);
		User user = User.findById(userId);

		List<Organization> userOrganizations = user.UsersOrg;
		List<Organization> joined = user.organization;
		List<Organization> subscribed = user.subscribedorgs;
		render(organizationList, joined, subscribed, userOrganizations);

	}

	/**
	 * renders the inviteUnregistered.html page in the organization
	 * 
	 * @param organizationId
	 * @author Sara Assem
	 */
	public static void inviteUnregistered(long organizationId) {

		render(organizationId);
	}

	/**
	 * 
	 * calls the method inviteUnregisteredOrganizer in the organization model
	 * and calls the invitationSent.html page in the organization
	 * 
	 * @param organizationId
	 * @param type
	 * @param chosen
	 * @author Sara Assem
	 * @throws EmailException
	 */
	public static void inivtationSentOrganizer(Long organizationId,
			String type, @Required String chosen) throws EmailException {

		String unemail = params.get("email");
		Organization organization = Organization.findById(organizationId);
		String idString = session.get("userId");
		if (idString == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(idString);
		User user = User.findById(userId);

		Long entityId = Entities.compareNames(chosen, organizationId);

		Entities entityTest = Entities.findById(entityId);
		System.out.println("new mal");
		Boolean exists = User.compareEmail(unemail);
		Boolean exists2 = UnregisteredInvitation.compareInvitation(unemail,
				user, type, organizationId);
		if (exists == true) {
			flash.error("This user is already registered already on our system");
			Organizations.inviteOrganizer(organizationId);
		}
		if (exists2 == false) {
			flash.error("You already sent an invitation to this email");
			Organizations.inviteOrganizer(organizationId);
		}
		organization.InviteUnRegisteredUsertobeOrganizer(unemail, user, type,
				entityTest.id);

		invitationSent(organizationId);

	}

	/**
	 * 
	 * calls the method inviteUnregisteredID in the organization model and calls
	 * the invitationSent.html page in the organization
	 * 
	 * @param organizationId
	 * @param type
	 * @author Sara Assem
	 * @throws EmailException
	 */

	public static void invitationSentID(Long organizationId, String type)
			throws EmailException {

		String unemail = params.get("email");
		Organization organization = Organization.findById(organizationId);
		String idString = session.get("userId");
		if (idString == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(idString);
		User user = User.findById(userId);
		Boolean exists = User.compareEmail(unemail);
		Boolean exists2 = UnregisteredInvitation.compareInvitation(unemail,
				user, type, organizationId);
		System.out.println(exists);
		System.out.println(exists2);
		if (exists == true) {
			flash.error("This user is already registered already on our system");
			Organizations.inviteID(organizationId);
		}
		if (exists2 == false) {
			flash.error("You already sent an invitation to this email");
			Organizations.inviteID(organizationId);
		}
		organization
				.InviteUnRegisteredUser(unemail, user, type, organizationId);
		invitationSent(organizationId);

	}

	/**
	 * 
	 * renders the inviteSent.html page in the organization
	 * 
	 * @param organizationId
	 */

	public static void invitationSent(Long organizationId) {

		render(organizationId);

	}

	/**
	 * renders the html page to fill in the email and chose entity for inviting
	 * UnregisteredEmail to be organizer
	 * 
	 * @param organizationId
	 * @author Sara Assem
	 */

	public static void inviteOrganizer(Long organizationId) {

		Organization organization = Organization.findById(organizationId);
		List<Entities> entity = organization.allEntities;
		String type = "organizer";
		render(organizationId, entity, type);
	}

	/**
	 * renders the html page to fill in the email for inviting UnregisteredEmail
	 * to be idea developer
	 * 
	 * @param organizationId
	 * @author Sara Assem
	 */
	public static void inviteID(Long organizationId) {
		String type = "ideaDeveloper";
		render(organizationId, type);
	}

	/**
	 * takes the organization id and shows the appropiate values of the selected
	 * organization to edit
	 * 
	 * @param Long
	 *            organizationId of the organization
	 * @author Mohamed Abdel Kader
	 * 
	 */

	public static void editOrganization(Long organizationId, String message) {

		Organization organization = Organization.findById(organizationId);
		render(organization, message);
	}

	/**
	 * takes the organization id and find the appropiate organization then gets
	 * the parameters from the HTML and check for name duplicates if there is
	 * duplicates it refreshes the page with appropiate message if not it
	 * adjusts the new values and save it to the DB
	 * 
	 * @param Long
	 *            organizationId of the organization
	 * @author Mohamed Abdel Kader
	 * 
	 */
	public static void organizationEdited(Long organizationId) {
		Organization organization = Organization.findById(organizationId);
		String name = params.get("name");

		boolean checkName = Organization.checkNameDuplication(name,
				organizationId);

		if (checkName == true) {
			organization.name = name;
			organization.email = params.get("email");
			organization.address = params.get("address");
			organization.description = params.get("description");
			organization.type = params.get("type");
			organization.category = params.get("category");
			String tagging = params.get("tag");
			organization.tagging = Boolean.parseBoolean(tagging);
			organization.save();
			Organizations.editOrganization(organizationId,
					"Changes are saved successfuly");
		}

		if (checkName == false) {
			flash.error("This name already exists");
			Organizations.editOrganization(organizationId, null);

		}
	}

	/**
	 * adds a user to subscriber list
	 * 
	 * @author Sara Assem
	 * @params Long id id of the organization
	 */
	public static void addSubscriber(Long organizationid) {
		Organization organization = Organization.findById(organizationid);
		String idString = session.get("userId");
		if (idString == null) {
			Home.home(null, null, "Please Login");
		}
		Long userid = Long.parseLong(idString);
		User user = User.findById(userid);
		user.subscribedorgs.add(organization);
		organization.addUserToSubscribers(user);
		organization.save();
		user.save();
		showOrganization(organizationid);

	}

	/**
	 * removes user from subscriber list
	 * 
	 * @author Sara Assem
	 * @params Long id id of the organization
	 */

	public static void removeSubscriber(Long organizationid) {
		Organization organization = Organization.findById(organizationid);
		String idstring = session.get("userId");
		if (idstring == null) {
			Home.home(null, null, "Please Login");
		}
		Long userid = Long.parseLong(idstring);
		User user = User.findById(userid);
		organization.removeUserFromSubscribers(user);
		user.subscribedorgs.remove(organization);
		organization.save();
		user.save();
		showOrganization(organizationid);

	}

	/*
	 * public void blockIdeaDeveloper(Long user_id, Long organization_id) {
	 * 
	 * String usertype = Permission.userPermissionInOrganization(user_id,
	 * organization_id); if (usertype == "IdeaDeveloper") { usertype =
	 * "blocked";
	 * 
	 * } }
	 */

	public static void block() {

		render();

	}

	/**
	 * This method renders page userblocked.html to view text inside it
	 * 
	 * The organization id
	 * 
	 * @author noha wafa
	 */
	public static void userblocked(User user) {

		render();
	}

	/**
	 * renders all entities available in a certain organization
	 * 
	 * @param organizationId
	 *            The organization id
	 * @author Mai Abu-El Seoud
	 */

	public static void viewAllEntities(Long organizationId) {

		Organization organization = Organization.findById(organizationId);
		List<Entities> allEntities = organization.getEntities();
		String idString = session.get("userId");
		if (idString == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(idString);
		User user = User.findById(userId);

		String role = "";
		Permission permission = Permission.find("user=? AND aUserType=?", user,
				"systemAdmin").first();
		if (permission != null) {
			role = permission.aUserType;
		}
		if (permission == null && role == "") {
			permission = User.userPermissionInOrganization(userId,
					organizationId);
			role = permission.aUserType;
		}

		render(allEntities, organization, role);
	}

	/**
	 * this method checks if a user is already a member in the organization if
	 * yes , it will render but if no , it will call addToOrganization
	 * 
	 * @param organizationId
	 *            the organization id
	 * @author Mai Abu-El Seoud
	 */
	public static void addMember(Long organizationId) {
		Organization organization = Organization.findById(organizationId);
		String idString = session.get("userId");
		if (idString == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(idString);
		User user = User.findById(userId);
		if (organization.checkUser(user)) {
			showOrganization(organizationId);
		} else {
			// addToOrganization(organizationId);
			user.organization.add(organization);
			organization.addUserToOrganization(user);
			organization.save();
			user.save();
			Permission permission = new Permission("ideaDeveloper",
					organization, user, null).save();
			showOrganization(organizationId);
		}
	}

	/**
	 * this method adds a certain user to a specific organization and renders a
	 * HTML page
	 * 
	 * @param organizationId
	 *            The organization id
	 * @author Mai Abu-El Seoud
	 */

	public static void addToOrganization(Long organizationId) {
		Organization organization = Organization.findById(organizationId);
		String idString = session.get("userId");
		if (idString == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(idString);
		User user = User.findById(userId);
		user.organization.add(organization);
		organization.addUserToOrganization(user);
		organization.save();

		Permission permission = new Permission("ideaDeveloper", organization,
				user, null).save();

		showOrganization(organizationId);
	}

	/**
	 * Allows the user to unjoin an organization that he is already a member in
	 * it.
	 * 
	 * @param orgid
	 *            The organization id
	 * @author Mai Abu-El Seoud
	 */
	public static void unjoinOrganization(Long organizationId) {
		Organization organization = Organization.findById(organizationId);
		String idString = session.get("userId");
		if (idString == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(idString);
		User user = User.findById(userId);
		user.organization.remove(organization);
		organization.removeUser(user);
		organization.save();
		user.save();
		for (int i = 0; i < organization.allEntities.size(); i++) {
			for (int j = 0; j < organization.allEntities.get(i).entityOrganizers
					.size(); j++) {
				if (organization.allEntities.get(i).entityOrganizers.get(j) == user) {
					organization.allEntities.get(i).entityOrganizers.remove(j);
					organization.allEntities.get(i).save();
				}
			}
		}
		int index = 0;
		for (int i = 0; i < user.permission.size(); i++) {
			if (user.permission.get(i).organization == organization) {
				index = i;
			}
		}
		user.permission.get(index).organization = null;
		user.save();
		user.permission.get(index).delete();
		showOrganization(organizationId);
	}

	/**
	 * adds the user to the list of requesters on the organization
	 * 
	 * 
	 * @param organizationId
	 *            the organization id
	 * @author Mai Abu-El Seoud
	 */
	public static void request(Long organizationId) {

		String idString = session.get("userId");
		if (idString == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(idString);
		User user = User.findById(userId);
		Organization organization = Organization.findById(organizationId);
		organization.requesters.add(user);

		user.orgRequested.add(organization);
		organization.save();
		// user.save();
		render();
	}

	/**
	 * renders a page to dispaly an error message for the user if he requested
	 * to join a private organization more than once
	 * 
	 * @author Mai Abu-El Seoud
	 */

	public static void requestedBefore() {
		render();
	}

	/**
	 * takes as input id of draft that will be published as topic and gets list
	 * of organizations of certain user
	 * 
	 * @param draftId
	 *            id of draft that will be published
	 * 
	 * @author Heba Khattab
	 */

	public static void allOrgOfUser(Long draftId) {
		String id = session.get("userId");
		if (id == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(id);
		User user = User.findById(userId);
		List organization = user.getOrganization();
		render(organization, draftId);
	}

	/**
	 * takes as input id of draft that will be published as idea and gets list
	 * of organizations of certain user
	 * 
	 * @param draftId
	 *            id of draft that will be published
	 * 
	 * @author Heba Khattab
	 */

	public static void allOrgOfUserTopic(Long draftId) {
		String id = session.get("userId");
		if (id == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(id);
		User user = User.findById(userId);
		List organization = user.getOrganization();
		render(organization, draftId);

	}

	/**
	 * Shows the conrtol panel of the organization for the organization lead to
	 * add/edit/delete prefrences of the organization
	 * 
	 * @param organiztionId
	 *            to render the conrtol panel of specific organization
	 * @author Mohamed Abdel Kader
	 * 
	 */
	public static void showControlPanel(Long organiztionId) {

		Organization organization = Organization.findById(organiztionId);

		render(organization);

	}

	/**
	 * renders a list of users who requested to join a private organization
	 * 
	 * 
	 * @param organizationId
	 *            The private organization id in which the list of requesters
	 *            will be rendered.
	 * @author Mai Abu-El Seoud
	 */

	public static void requestersList(Long organizationId) {
		Organization organization = Organization.findById(organizationId);
		List<User> requesters = organization.requesters;
		render(requesters, organization);
	}

	/**
	 * takes the organization id and renders page showIdeaDevelopersInOrg.html
	 * that shows all the users in organization and loops on the list of entity
	 * organizers of all entities and removes them from the list of users shown
	 * here in order to not be able to block except idea developers
	 * 
	 * @see showAllIdeaDeveloper.html
	 * 
	 * @param long orgId Id of the organization
	 * 
	 * @author noha wafa
	 * 
	 * 
	 * 
	 */
	public static void showIdeaDevelopersInOrg(Long orgId) {

		Organization organization = Organization.findById(orgId);
		List<User> users = organization.getOrgMembers();

		// for(int i=0;i<organization.getEntities().size();i++)
		// for(intj=0;j<)

		render(users, organization);
	}

	/**
	 * This method renders page userblocked.html to view text inside it
	 * 
	 * The organization id
	 * 
	 * @author noha wafa
	 */
	/*
	 * public static void blockUser(Long orgId) {
	 * 
	 * System.out.println(orgId); orgId=(long) 2; String []
	 * selectedUsers=params.getAll("box"); List<String> list =
	 * Arrays.asList(selectedUsers);
	 * 
	 * for(int i=0;i<list.size();i++){
	 * 
	 * list.get(i).
	 * 
	 * }
	 * 
	 * 
	 * Organization org= Organization.findById(orgId); List<User> orgUsers =
	 * org.getOrgMembers();
	 * 
	 * render(users);
	 * 
	 * 
	 * }
	 */
	/**
	 * takes the user from the list and renders the name of the user who will be
	 * blocked and add the user to a list of blocked users and remove him from
	 * org members list
	 * 
	 * @see blockUser.html
	 * 
	 * @param String
	 *            name which is the name of the user that has been blocked
	 * @param Long
	 *            userId which is user id which we wish to have him blocked
	 * @param Long
	 *            id which is the organization id whom would be blocked from
	 * 
	 * @author noha wafa
	 * 
	 * 
	 * 
	 */
	public static void blockUser(String name, Long id, Long userId) {
		User user = User.findById(userId);
		Organization organization = Organization.findById(id);
		organization.orgMembers.remove(user);
		organization.blockedUsers.add(user);
		/*
		 * List<Permission>
		 * permission=Permission.find("USER_ID=? AND ORGANIZATION_ID=?", userId,
		 * organization).fetch(); for(int i=0;i<permission.size();i++)
		 * permission.get(0).delete();
		 */

		organization.save();
		String idstring = session.get("userId");
		Long usersId = Long.parseLong(idstring);
		User lead = User.findById(usersId);
		List<User> users = organization.getOrgMembers();
		lead.sendNotificationWithType("user" + user.name + "has been blocked",
				users, organization.name, organization.id);
		User leader = User.findById(usersId);
		ArrayList<User> blockedMember = new ArrayList<User>();
		blockedMember.add(user);
		leader.sendNotificationWithType(
				" you are now blockd due to your bad attitude", blockedMember,
				organization.name, organization.id);
		// lead.sendNotificationWithType("user" + user.name +"has been blocked",
		// organization.orgMembers, type, typeId);
		// organization.orgMembers.notify();
		render(name, organization);

	}

	/**
	 * approves the request of a developer to join a private organization
	 * 
	 * @param Long
	 *            userId which is the user id that requested to join
	 * @param Long
	 *            organizationId which is organization id which the user
	 *            requests to join
	 * 
	 * @author Amr El Salanekly
	 * 
	 */
	public static void approveRequest(Long userId, long organizationId) {
		User user = User.findById(userId);
		Organization organization = Organization.findById(organizationId);
		organization.addUserToOrganization(user);
		organization.save();
		user.organization.add(organization);
		user.save();
		List<User> users = new ArrayList();
		users.add(user);

		for (int i = 0; i < organization.requesters.size(); i++) {
			if (organization.requesters.get(i) == user) {
				organization.requesters.remove(i);
			}

		}
		organization.save();
		String idstring = session.get("userId");
		if (idstring == null) {
			Home.home(null, null, "Please Login");
		}
		Long usersId = Long.parseLong(idstring);
		User lead = User.findById(usersId);
		lead.sendNotificationWithType("you are now a member in organization",
				users, "Organization", organization.id);
		requestersList(organizationId);
	}

	/**
	 * Rejects the request of a developer to join a private organization
	 * 
	 * @param Long
	 *            userId which is the user id that requested to join
	 * @param Long
	 *            organizationId which is organization id which the user
	 *            requests to join
	 * 
	 * @author Amr El Salanekly
	 * 
	 */
	public static void rejectRequest(long userId, long organizationId) {
		Organization organization = Organization.findById(organizationId);
		List<User> users = new ArrayList();
		User user = User.findById(userId);
		for (int i = 0; i < organization.requesters.size(); i++) {
			if (organization.requesters.get(i) == user) {
				organization.requesters.remove(i);
			}

		}
		organization.save();
		String idstring = session.get("userId");
		if (idstring == null) {
			Home.home(null, null, "Please Login");
		}
		Long usersId = Long.parseLong(idstring);
		User lead = User.findById(usersId);
		lead.sendNotificationWithType("you are now a member in organization",
				users, "Organization", organization.id);
		requestersList(organizationId);
	}

	/**
	 * approves a request from a user who has been blocked before
	 * 
	 * @param userId
	 *            The user ID who was blocked
	 * @param organizationId
	 *            The organizationId that he/she requests to re-join
	 * 
	 * @author Mai Abu-El Seoud
	 */

	public static void approveBlockedRequest(Long userId, long organizationId) {
		User user = User.findById(userId);
		Organization organization = Organization.findById(organizationId);
		organization.addUserToOrganization(user);
		organization.save();
		user.organization.add(organization);
		user.save();
		List<User> users = new ArrayList();
		users.add(user);

		for (int i = 0; i < organization.blockedRequesters.size(); i++) {
			if (organization.blockedRequesters.get(i) == user) {
				organization.blockedRequesters.remove(i);
			}

		}
		for (int i = 0; i < organization.blockedUsers.size(); i++) {
			if (organization.blockedUsers.get(i) == user) {
				organization.blockedUsers.remove(i);
			}
		}
		organization.save();
		String idstring = session.get("userId");
		Long usersId = Long.parseLong(idstring);
		User lead = User.findById(usersId);
		lead.sendNotificationWithType(
				"you are now a member in organization again!!", users,
				"Organization", organization.id);
		blockedList(organizationId);
	}

	/**
	 * adds the user to the list of blocked requesters on the organization
	 * 
	 * 
	 * @param organizationId
	 *            the organization id
	 * @author noha wafa
	 */
	public static void blockedRequest(Long organizationId) {

		String idString = session.get("userId");
		if (idString == null) {
			Home.home(null, null, "Please Login");
		}
		Long userId = Long.parseLong(idString);
		User user = User.findById(userId);
		Organization organization = Organization.findById(organizationId);
		organization.blockedRequesters.add(user);

		user.blocked.add(organization);
		organization.save();

		render();
	}

	/**
	 * renders a list of users who requested to be unblocked after being blocked
	 * from organization
	 * 
	 * 
	 * @param organizationId
	 *            The organization id in which the list of bloked requesters
	 *            will be rendered.
	 * @author noha wafa
	 */
	public static void blockedList(Long organizationId) {
		Organization organization = Organization.findById(organizationId);
		List<User> list = organization.blockedRequesters;
		render(list, organization);
	}

	/**
	 * rejects a member's request to re-join the organization
	 * 
	 * @param userId
	 *            The user id that requests to re-join
	 * @param organizationId
	 *            The organization that he/she requests to re-join
	 * @author Mai Abu-El Seoud
	 */
	public static void rejectBlockRequest(long userId, long organizationId) {
		Organization organization = Organization.findById(organizationId);
		List<User> users = new ArrayList();
		User user = User.findById(userId);
		for (int i = 0; i < organization.blockedRequesters.size(); i++) {
			if (organization.blockedRequesters.get(i) == user) {
				organization.blockedRequesters.remove(i);
			}
		}

		organization.save();
		String idstring = session.get("userId");
		Long usersId = Long.parseLong(idstring);
		User lead = User.findById(usersId);
		lead.sendNotificationWithType(
				"your request was rejected , to join the organization again",
				users, "Organization", organization.id);
		blockedList(organizationId);
	}
}
